package com.hkts.naturalgas.performance.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hkts.naturalgas.feign.FeignFlow;
import com.hkts.naturalgas.performance.entity.HkAssessmentInfo;
import com.hkts.naturalgas.performance.performanceVo.AssessmentDetilResultVo;
import com.hkts.naturalgas.performance.performanceVo.AssessmentDetilVo;
import com.hkts.naturalgas.performance.performanceVo.ProcessDateAndEmpNumVo;
import com.hkts.naturalgas.performance.performanceVo.ProcessNodesUserAndTime;
import com.hkts.naturalgas.performance.service.IHkAssessmentInfoService;
import com.hkts.naturalgas.utils.CustomPage;
import com.hkts.naturalgas.utils.CustomPageUtils;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yck
 * @since 2020-12-23
 */
@RestController
@RequestMapping("/performance/hkAssessmentInfo")
public class HkAssessmentInfoController {
    private final Logger logger = LoggerFactory.getLogger(HkAssessmentInfoController.class);

    @Autowired
    public IHkAssessmentInfoService iHkAssessmentInfoService;

    @Autowired
    private FeignFlow feignFlow;

    /**
     * 分页查询数据
     *
     * @param page             分页信息
     * @param hkAssessmentInfo 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHkAssessmentInfoPageList")
    public Result<Object> getHkAssessmentInfoPageList(Page<HkAssessmentInfo> page, HkAssessmentInfo hkAssessmentInfo) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHkAssessmentInfoService.selectPage(page, hkAssessmentInfo)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hkAssessmentInfo 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkAssessmentInfoSave")
    public Result<Object> hkAssessmentInfoSave(HkAssessmentInfo hkAssessmentInfo) {
        iHkAssessmentInfoService.saveOrUpdate(hkAssessmentInfo);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hkAssessmentInfoGetById")
    public Result<Object> hkAssessmentInfoGetById(String id) {
        HkAssessmentInfo byId = iHkAssessmentInfoService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkAssessmentInfoDelete")
    public Result<Object> hontUserDelete(String id) {
        iHkAssessmentInfoService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkAssessmentInfoBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHkAssessmentInfoService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }


    /*
     * 根据流程id获取数据
     *
     * */

    @ResponseBody
    @PostMapping("/getHkAssessmentInfoByProIds")
    public List<Map> getHkAssessmentInfoByProIds(@RequestBody List<String> ids) {
        List<Map> listResult = new ArrayList<>();
        List<HkAssessmentInfo> list = iHkAssessmentInfoService.getHkAssessmentInfoByProIds(ids);
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", list.get(i).getId());
            map.put("createBy", list.get(i).getId());

            map.put("delFlag", list.get(i).getDelFlag());
            map.put("updateBy", list.get(i).getUpdateBy());

            map.put("assessmentName", list.get(i).getAssessmentName());

            map.put("assessmentStatus", list.get(i).getAssessmentStatus());
           /* map.put("createTime",list.get(i).getCreateTime());
            map.put("updateTime",list.get(i).getUpdateTime());
            map.put("endTime",list.get(i).getEndTime());*/
            map.put("processInstanceId", list.get(i).getProcessInstanceId());
            listResult.add(map);
        }
        return listResult;
    }

    /*
     * 根据流程实例查询流程提交的时间和用户
     * */
    @ResponseBody
    @GetMapping("/getApparoleUserIdAndTimeByProcessId")
    public Result getApparoleUserIdAndTimeByProcessId(String processInstantId) {
        List<ProcessNodesUserAndTime> list = iHkAssessmentInfoService.getApparoleUserIdAndTimeByProcessId(processInstantId);
        return ResultUtil.data(list);
    }
    /*
     * 查询考核月份
     * */
    @ResponseBody
    @GetMapping("/getAssessmentMonth")
    public Result getAssessmentMonth(CustomPage page,String nickName,String businessType,String departId) throws Exception {
        return iHkAssessmentInfoService.getAssessmentMonth(page,nickName,businessType,departId);

    }
    /*
     * 根据考核月份获得本月考核数据
     * */
    @ResponseBody
    @GetMapping("/getAssessmentDatesByMonth")
    public Result getAssessmentDates(String month,String nickName,String businessType,String departId) throws Exception {
        return iHkAssessmentInfoService.getAssessmentDatesByMonth(month,nickName,businessType,departId);

    }

    /*
     * 根据日期查询考核情况
     * */
    @ResponseBody
    @GetMapping("/getAssessmentDates")
    public Result getAssessmentDates(CustomPage page,String nickName,String businessType,String departId) throws Exception {
        return iHkAssessmentInfoService.getAssessmentDates(page,nickName,businessType,departId);

    }


    /*
     * 根据日期查询考核详情
     * */
    @ResponseBody
    @GetMapping("/getAssessmentDetil")
    public Result getAssessmentDetil(String date,String nickName,String businessType,String departId) throws Exception {
        List<AssessmentDetilResultVo> list = iHkAssessmentInfoService.getAssessmentDetil(date,nickName,businessType,departId);
        return ResultUtil.data(list);
    }

    /*
    * 获取日期查询
    * */
    @ResponseBody
    @GetMapping("/getAssessmentDetilList")
    public Result getAssessmentDetilList(String date) throws Exception {
        String listApplyedTasks = feignFlow.getListApplyedTasks(date);
        return ResultUtil.data(listApplyedTasks);
    }
    /*
     * 处理历史数据
     * */
    @ResponseBody
    @GetMapping("/historyData")
    public Result historyData(String date) throws Exception {
        iHkAssessmentInfoService.historyData();
        return ResultUtil.success("处理完成");
    }
}